//引入缓存
import { Session } from '@store/cache/index';
import { useUserInfoStore } from '@store/user_info';
let userInfoStore = undefined;
export function getUserInfoStore() {
	if (!userInfoStore) {
		userInfoStore = useUserInfoStore();
	}
	return userInfoStore;
}
function getInervalHour(startDate, endDate) {
	var ms = endDate.getTime() - startDate.getTime();
	if (ms < 0) return 0;
	return Math.floor(ms / 1000 / 60 / 60);
}
function getCurrentFullToken(){
	let userInfo = getUserInfoStore().userInfo;
	if (userInfo && userInfo.userToken) {
		return userInfo.userToken;
	}
	return null;
}

function getCurrentTokenDetail(key){
	let userInfo = getUserInfoStore().userInfo;
	if (userInfo && userInfo.userToken) {
		return userInfo.userToken[key];
	}
	return null;
}
export const SignUtil = {
	getUserId: () => {
		let userInfo = getUserInfoStore().userInfo;
		if (userInfo && userInfo.userId) {
			return userInfo.userId;
		}
		return null;
	},
	getUserName: () => {
		let userInfo = getUserInfoStore().userInfo;
		if (userInfo && userInfo.userDetail && userInfo.userDetail.userName) {
			return userInfo.userDetail.userName;
		}
		return null;
	},
	isSupperManager: () => {
		let userInfo = getUserInfoStore().userInfo;
		if (userInfo && true==userInfo.isSuperManager) {
				return true;
		}
		return false;
	},
	getUserSigninInfo: () => {
		let userInfo = getUserInfoStore().userInfo;
		if (userInfo && userInfo.userSignin) {
			return userInfo.userSignin;
		}
		return null;
	},
	getUserDetail: () => {
		let userInfo = getUserInfoStore().userInfo;
		if (userInfo && userInfo.userDetail) {
			return userInfo.userDetail;
		}
		return null;
	},
	isSignin: () => {
		let userInfo = getUserInfoStore().userInfo;
		if (userInfo && userInfo.userId &&userInfo.userToken && userInfo.lastRefreshTime) {
			return true;
		}
		return false;
	},
	needRefreshToken:()=>{
		let userInfo = getUserInfoStore().userInfo;
		let _expires_in =getCurrentTokenDetail("expires_in");
		if (userInfo && userInfo.userId && userInfo.lastRefreshTime) {
			let _lastRefreshTime = userInfo.lastRefreshTime.getTime();
			let _now=(new Date()).getTime();
			let _dur=(_now-_lastRefreshTime)/1000;//计算多少秒没访问后端数据了
			if(!_expires_in){
				_expires_in=7200;
			}
			let _diff=_expires_in-_dur;//计算距离过期，还有多少秒
			let _stard=_expires_in/2;//默认判断标准为过期时间的一半。
			if (_stard>1800){//如果过期时间的一半超过半小时，就按半小时算
				_stard=1800;
			}
			if (_diff<=_stard) {//超过有效期了
				return true;
			}
		}
		return false;
	},
	getUserToken: () => {
		return getCurrentFullToken();
	},
	getAccessToken: () => {
		let _type=getCurrentTokenDetail('token_type');
		return _type+" "+ getCurrentTokenDetail('access_token');
	},
	getRefreshToken: () => {
		return getCurrentTokenDetail('refresh_token');
	},
	setSignin:(userSignin, loginTime)=>{
		getUserInfoStore().setSigninInfo(userSignin, loginTime);
		return getUserInfoStore().userInfo;
	},
	saveUser: ( isSuperManager,userDetail) => {
		getUserInfoStore().setUserInfo(isSuperManager,userDetail);
		return getUserInfoStore().userInfo;
	},
	refreshToken:(userToken)=>{
		getUserInfoStore().refreshToken(userToken);
	},
	clearUser: () => {
		getUserInfoStore().clearUserInfo();
	}
};
